<?php
declare (strict_types=1);

namespace app\admin\controller;

use think\App;
use think\facade\View;
use app\common\model\Area;
use app\common\facade\Builder;
use think\exception\ValidateException;
use think\response\Json;

class CurriculumOrder extends Base
{

    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\CurriculumOrder';

    //关联模型
    protected array $hasOne = ['user'];

    protected int $takeOver = 3;

//    protected array $append = ['is_refunds'];

    public function index(): string
    {
        return $this->setTable([]);
    }

    public function setTable($extra_map = []): string
    {
        $table =  Builder::Table()
            ->addColumn(['type' => 'checkbox', 'fixed' => 'left', 'title' => '全选'])
            ->addColumn(['field' => 'id', 'title' => '订单ID', 'width' => 90])
            ->addColumn(['field' => 'sn', 'title' => '订单编号'])
            ->addColumn(['field' => 'curriculum_name', 'title' => '课程名称'])
            ->addColumn(['field' => 'curriculum_image', 'title' => '课程图片'])
            ->addColumn(['field' => 'total_amount', 'title' => '订单金额'])
            ->addColumn(['field' => 'real_amount', 'title' => '实付金额'])
            ->addColumn(['field' => 'code', 'title' => '下单会员', 'templet' => '#_userTpl', 'width' => 250])
            ->addColumn(['field' => 'status', 'title' => '订单状态'])
            ->addColumn(['field' => 'create_time', 'title' => '下单时间'])
            ->addColumn(['field' => 'pay_time', 'title' => '支付时间'])
            ->addColumn(['field' => 'pay_way', 'title' => '支付方式'])
            ->addColumn(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#orderDemo', 'title' => '操作', 'width' => 150])
            ->addTopButtons(['deleteAll', 'export'])
            ->addRightButtons([
                'edit' => ['title' => '详情', 'size' => '1200,1080'],
                'delete'
            ]);
            return $table->addSearch([
                ['uid', '会员编号/昵称/手机号', 'input', '', '\app\common\model\User', 'code|nickname|mobile,id'],
                ['sn', '订单编号', 'input'],
                ['status', '订单状态', 'select', getOrderStatus('', 2)]
            ])
            ->addJsUrl(['order'])
            ->addExtraMap($extra_map)
            ->addExtraHtml(['order'])
            ->fetch();
    }

    public function edit($id = '', $tpl = 'curriculum_order/edit', $info = '')
    {
        return parent::edit($id, $tpl, $info); // TODO: Change the autogenerated stub
    }

    public function drawback($id = '')
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            try {
                return json($this->model::drawback($param, $this->admin));
            } catch (ValidateException $e) {
                return json(['code' => -1, 'msg' => $e->getError()]);
            }
        }
        $info = $this->model::find($id);
        View::assign('form', ['title' => '退款凭证', 'field' => 'refund_imgs', 'value' => 1]);
        View::assign([
            'info'    => $info
        ]);
        return View::fetch();
    }

    public function deliver($id = '')
    {

        if ($this->request->isPost()) {
            $param = $this->request->param();
            try {
                $this->validate($param, 'Deliver');
            } catch (ValidateException $e) {
                return json(['code' => -1, 'msg' => $e->getError()]);
            }
            return json($this->model::deliver($param, $this->admin));
        }

        $info = $this->model::find($id);

        View::assign([
            'express' => \app\common\model\Express::where(['status' => 1, 'is_del' => 0])->select(),
            'info'    => $info
        ]);
        return View::fetch();
    }

    public function getArea($id, $level = 0)
    {
        return Area::getArea($id ?: 100000, $level);
    }

}
